// https://leetcode.cn/problems/maximum-product-of-word-lengths/
// Created by ade on 2022/10/27.
//

class Solution {
public:
    int maxProduct(vector <string> &words) {
        vector<int> wordValue = {};
        for (auto &w:words) {
            int t = 0;
            for (auto &c:w)
                t |= (1 << (c - 'a'));
            wordValue.push_back(t);
        }
        int maxI = 0;
        for (int i = 0; i < words.size(); i++) {
            for (int j = i + 1; j < words.size(); j++) {
                if ((wordValue[i] & wordValue[j]) == 0) maxI = max(maxI, int(words[i].size() * words[j].size()));
            }
        }
        return maxI;
    }

    int maxProduct(vector <string> &words) {
        unordered_map<int, int> m = {};
        for (auto &w:words) {
            int t = 0;
            for (auto &c:w)
                t |= (1 << (c - 'a'));
            if (m.count(t) > 0) m[t] = max(m[t], int(w.size()));
            else m[t] = w.size();
        }
        int maxI = 0;
        for (auto &i : m) {
            for (auto &j :m) {
                if ((i.first & j.first) == 0) maxI = max(i.second * j.second, maxI);
            }
        }
        return maxI;
    }
};